<?php

namespace common\models\course;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%recommend_course}}".
 *
 * @property int $id
 * @property string $title 推荐内容标题
 * @property string $description 推荐描述
 * @property string $banner 推荐封面横图
 * @property string $cover 推荐展示封面
 * @property int $category_id 推荐课程类别ID
 * @property int $course_id 推荐课程ID
 * @property int $weight 权重
 * @property int $listorder 推荐排序
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class RecommendCourse extends \common\base\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;
    use \common\traits\QueryInstanceActiveRecord;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%recommend_course}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['title', 'banner', 'cover'], 'required'],
            [['category_id', 'course_id', 'weight', 'listorder', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title'], 'string', 'max' => 100],
            [['description', 'banner', 'cover'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'title' => '推荐内容标题',
            'description' => '推荐描述',
            'banner' => '推荐封面横图',
            'cover' => '推荐展示封面',
            'category_id' => '推荐课程类别ID',
            'course_id' => '推荐课程ID',
            'weight' => '权重',
            'listorder' => '推荐排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getCourse() {
        return $this->hasOne(Course::className(), ['id' => 'course_id']);
    }

    public function getCategory() {
        return $this->hasOne(CourseCategory::className(), ['id' => 'category_id']);
    }

    public function extraFields() {
        return [
            'course' => function() {
                return $this->course;
            },
            'link' => function() {
                return $this->link;
            },
            'banner' => function() {
                return Url::toRemote($this->banner);
            },
            'thumb' => function() {
                return Url::toRemote($this->thumb);
            },
        ];
    }

    public function getLink() {
        return Url::h5('/course/' . $this->course_id);
    }

}
